/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datamodel.GraduationSchool;
import pt.iasd.youthshelter.datamodel.service.GraduationSchoolController;
import pt.iasd.youthshelter.wizards.core.events.graduation.GraduationSchoolTablePanel;

@ActionID(
        category = "Management",
        id = "pt.iasd.youthshelter.wizards.core.actions.ShowGraduationSchoolPanelAction")
@ActionRegistration(
        iconBase = "pt/iasd/youthshelter/wizards/core/actions/escoladeformacao.png",
        displayName = "Graduation School")
@ActionReferences({
    @ActionReference(path = "Menu/Management", position = 500, separatorAfter = 550),
    @ActionReference(path = "Toolbars/Management", position = 500)
})
public final class ShowGraduationSchoolPanelAction implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void actionPerformed(ActionEvent e) {
        ProgressHandle pHandle = ProgressHandleFactory.createHandle(
                    bundle.getString("ProgressBarWizard"));
        pHandle.start();
        final GraduationSchoolTablePanel graduationSchoolPanel = new GraduationSchoolTablePanel();
        DialogDescriptor dialog = new DialogDescriptor(graduationSchoolPanel, bundle.getString("GraduationSchool.Panel"), true, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == DialogDescriptor.OK_OPTION) {
                    Set<Integer> edited = graduationSchoolPanel.getEditedGraduationSchools();
                    Set<Integer> removed = graduationSchoolPanel.getRemovedGraduationSchools();
                    GraduationSchoolController controller = Lookup.getDefault().lookup(GraduationSchoolController.class);
                    for(GraduationSchool graduationSchool : graduationSchoolPanel.getGraduationSchools()) {
                        if(graduationSchool.getId() == null) {
                            controller.create(graduationSchool);
                        } else {
                            if(removed.contains(graduationSchool.getId())) {
                                graduationSchool.setEndDate(new Date());
                                edited.add(graduationSchool.getId());
                            }
                            if(edited.contains(graduationSchool.getId())) {
                                try {
                                    controller.edit(graduationSchool);
                                } catch (NonexistentEntityException ex) {
                                    controller.create(graduationSchool);
                                } catch (Exception ex) {
                                    logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                                    NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                        String.format(bundle.getString("GraduationSchool.Save.Error")
                                                .replace("\n", System.getProperty("line.separator")),
                                            ex.getLocalizedMessage()),
                                            NotifyDescriptor.ERROR_MESSAGE);
                                    DialogDisplayer.getDefault().notify(dialog);
                                }
                            }
                        }
                    }
                }
            }
        });
        pHandle.finish();
        DialogDisplayer.getDefault().notify(dialog);
    }
}
